<?php
namespace app\admin\model;

use think\Model;

class Keywords extends Model{

    private $model;
    public function __construct(){
        parent::__construct();
        $this -> model = db('keywords');
    }
    /*  查询数据详情
     *  @param  Array   [where]
     *  @return Array   [info]
     * **/
    public function getDetailByWhere($where){
        $result = $this -> model -> where($where) -> find();
        return $result;
    }
    /*  分页查询关键字列表
     *  @param  Array   [where]
     *  @param  string  &page
     *  @return $list
     * **/
    public function getKeywordsListByWhere($where,$num,&$page){
        $modelData = $this -> model -> where($where) -> order('id asc') -> paginate($num,false,['query'=>request()->param() ]);
        $page = $modelData -> render();
        $data = iterator_to_array($modelData);
        if(empty($data)){   return [];  }
        //格式化数据
        $list = $this -> formatKeywordsDataForLIst($data);
        return $list;
    }
    /*  添加数据
     *  @param  Array   [data]
     *  @return     res result
     * **/
    public function add($data){
        $result = $this -> model -> insertGetId($data);
        return $result;
    }
    /*  修改数据
     *  @param  Array   [where]
     *  @param  Array   [save]
     *  @return res     [result]
     * **/
    public function modify($where,$save){
        $result = $this -> model -> where($where) -> update($save);
        return $result;
    }
    /*  格式化关键字列表数据
     *  @param  Array   [data]
     *  @return Array   [list]
     * **/
    public function formatKeywordsDataForLIst($data){
        foreach ($data as $key => $value) {
            $list[$key]['id'] = $value['id'];
            $list[$key]['mode'] = $value['mode'];
            $list[$key]['title'] = $value['title'];
            $list[$key]['keyword'] = $value['keyword'];
            $list[$key]['description'] = $value['description'];
            $list[$key]['param_1'] = $value['param_1'];
            $list[$key]['param_2'] = $value['param_2'];
        }
        return $list;
    }


}